/**
 * Copyright (c) Css Team
 * All rights reserved.
 *
 * This file AddOrg.java 
 * http://www.css.com.cn
 */
package com.css.apps.base.org.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.common.Constants;
import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.org.model.SOrg;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Json;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

@SuppressWarnings("serial")
public class AddOrg extends AdminAction implements ModelDriven {
	
	private static Log log = LogFactory.getLog(AddOrg.class);
	private SOrg item;

	public AddOrg() {
		this.item = new SOrg();
	}

	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(item.getName())) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			Tree tree = new TreeCache(SOrg.class);
			// TODO 观察是否能获取根节点
			TreeNode root = null;
			if (tree != null) {
				List<TreeNode> roots = tree.getRoot();
				if (ListUtil.isLstNotEmpty(roots)) {
					root = roots.get(0);
				}
			}
			List<TreeNode> allorglist = root == null ? new ArrayList<TreeNode>() : root.getAllChildren();
			for (TreeNode org : allorglist) {
				SOrg tmp = QueryCache.get(SOrg.class, org.getNodeId());
				if (StringHelper.isNotEmpty(item.getName()) && tmp.getName().equals(item.getName())) {
					TreeNode nodetmp = tree.getTreeNode(tmp.getUuid());
					if (nodetmp.getParent() != null && nodetmp.getParentId().equals(item.getParentId())) {
						result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
								Messages.getString("systemMsg.nameExist"));
						return Action.ERROR;
					}
				}
			}
			
			String parentId = item.getParentId();
			if (root.getNodeId().equals(parentId)) {
				item.setOrgLevel(2);
			} else {
				SOrg parent = QueryCache.get(SOrg.class, parentId);
				if (parent != null) {
					item.setOrgLevel(parent.getOrgLevel() + 1);
				}
			}
			Date now = Calendar.getInstance().getTime();
			item.setDelFlag(Constants.NO);
			// 添加组织机构的时间
			item.setEditDate(now);
			item.setIssueDate(now);
			item.setIssueId(sUser.getUserId());
			item.setIssueName(sUser.getRealName());
			tx = new TransactionCache();
			tx.save(item);
			tx.commit();

			tree.refresh();

			// 记录日志
			LogPart lp = new LogPart();
			lp.setOpObjType(SOrg.class.getName());
			lp.setOpObjId(item.getUuid());
			lp.setRelObjType(SOrg.class.getName());
			lp.setRelObjId(item.getUuid());
			lp.setOpType(LogConstant.LOG_TYPE_ADD);
			lp.setLogLevel(LogConstant.LOG_LEVEL_COMMON);
			lp.setLogData(Json.object2json(item));
			lp.setResult(LogConstant.RESULT_SUCCESS);
			lp.save();

			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"), item.toJson());
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}
	
	@Override
	public Object getModel() {
		return item;
	}

	public SOrg getItem() {
		return item;
	}

	public void setItem(SOrg item) {
		this.item = item;
	}

}
